### This code replicates Table 2 in "Moderates"
### April 25, 2022

library(tidyverse)
library(survey)
library(xtable)
library(haven)
library(foreign)

data<-read.dta('bigsurveys_recoded4.dta')
data$ideal_point<-scale(data$x)[,1]
data$weight<-as.numeric(as.vector(data$weight))

#RespondentTypes<-read_dta('RespondentTypes.dta')
dim(data)
#data<-merge(data,RespondentTypes, by=c("source", "respondent") )
dim(data)
est.groups<-1
if (est.groups==1){
group_by(data, source) %>%
	summarise(	
		mean(weight, na.rm=T))
#data<-filter(data, !is.na(weight))
data$weight[is.na(data$weight)]<-1	
data$group<-"unknown"

table(data$group)
data$group[data$w3>data$w1 &data$w3>data$w2 ]<-"inattentive"
data$group[data$w2>data$w1 &data$w2>data$w3 ]<-"conversian"
data$group[data$w1>data$w2 &data$w1>data$w3 ]<-"downsian"
table(data$group)

data$group2<-data$group


data<-group_by(data, source,group) %>%
	mutate(	
		rank = percent_rank(ideal_point)
		)

data<-group_by(data, source,group) %>%
  arrange(ideal_point) %>% 
  mutate(rank = lag(cumsum(weight), default = 0)/(sum(weight) - 1))


data<-ungroup(data)

summary(data$rank)

data$group2<-data$group
data$group2[data$group=="downsian" & data$rank<= 1/3]<-"liberal downsian"
data$group2[data$group=="downsian" & data$rank< 2/3 & data$rank> 1/3]<-"moderate downsian"
data$group2[data$group=="downsian" & data$rank>= 2/3]<-"conservative downsian"
table(data$group2)

}
data$group2 <- factor(data$group2, levels=c('conservative downsian','moderate downsian','liberal downsian','conversian','inattentive'))
  
data2<-filter(data, source=="CCES_2018" | source=="CCES_2016"| source=="CCES_2017")
srs_design_survey <- svydesign(ids = ~1, weights=data2$weight, data =data2 )

###
## This code displays the opinion results for each item in Table 2 of our paper
###

### Three Strikes and Out Prison Sent.
cces2016_increasesentences<-svymean(~cces2016_increasesentences, srs_design_survey, na.rm=T)
cces2016_increasesentences2<-svyby(~cces2016_increasesentences, ~group2, srs_design_survey, svymean, na.rm=T)
cces2016_increasesentences
xtable(cces2016_increasesentences2)

### Buy & Hire American
cces2017_buyamerican<-svymean(~cces2017_buyamerican, srs_design_survey, na.rm=T)
cces2017_buyamerican2<-svyby(~cces2017_buyamerican, ~group2, srs_design_survey, svymean, na.rm=T)
cces2017_buyamerican
xtable(cces2017_buyamerican2)

##Ban Late-Term Abortion
cces2014_abortionlateterm<-svymean(~cces2014_abortionlateterm, srs_design_survey, na.rm=T)
cces2014_abortionlateterm2<-svyby(~cces2014_abortionlateterm, ~group2, srs_design_survey, svymean, na.rm=T)
cces2014_abortionlateterm
xtable(cces2014_abortionlateterm2)

#Don’t Publish Gun Owner Names
cces2013_gunnames<-svymean(~cces2013_gunnames, srs_design_survey, na.rm=T)
cces2013_gunnames2<-svyby(~cces2013_gunnames, ~group2, srs_design_survey, svymean, na.rm=T)
cces2013_gunnames
xtable(cces2013_gunnames2)

#Don’t Use Medicaid for Abortion
cces2018_abortion_medicaid<-svymean(~cces2018_abortion_medicaid, srs_design_survey, na.rm=T)
cces2018_abortion_medicaid2<-svyby(~cces2018_abortion_medicaid, ~group2, srs_design_survey, svymean, na.rm=T)
cces2018_abortion_medicaid
xtable(cces2018_abortion_medicaid2)

##Repeal ACA
cces2012_repeal.aca<-svymean(~cces2012_repeal_aca, srs_design_survey, na.rm=T)
cces2012_repeal.aca
cces2012_repeal.aca2<-svyby(~cces2012_repeal_aca, ~group2, srs_design_survey, svymean, na.rm=T)
xtable(cces2012_repeal.aca2)

#Eliminate Income Tax
all_eliminate_incometax<-svymean(~cces2018_eliminate_incometax, srs_design_survey, na.rm=T)
all_eliminate_incometax
cces2018_eliminate_incometax2<-svyby(~cces2018_eliminate_incometax, ~group2, srs_design_survey, svymean, na.rm=T)
xtable(cces2018_eliminate_incometax2)

#Environmental Enforcement
cces2014_caaenforcement<-svymean(~cces2014_caaenforcement, srs_design_survey, na.rm=T)
cces2014_caaenforcement
cces2014_caaenforcement2<-svyby(~cces2014_caaenforcement, ~group2, srs_design_survey, svymean, na.rm=T)
xtable(cces2014_caaenforcement2)

#Withdraw Paris Climate Agreement
all_withdraw_paris_climate<-svymean(~cces2018_withdraw_paris_climate, srs_design_survey, na.rm=T)
all_withdraw_paris_climate
all_withdraw_paris_climate2<-svyby(~cces2018_withdraw_paris_climate, ~group2, srs_design_survey, svymean, na.rm=T)
xtable(all_withdraw_paris_climate2)

#Same-Sex Marriage
naes2004_stategaymarriage<-svymean(~naes2004_stategaymarriage, srs_design_survey, na.rm=T)
naes2004_stategaymarriage
naes2004_stategaymarriage2<-svyby(~naes2004_stategaymarriage, ~group2, srs_design_survey, svymean, na.rm=T)
xtable(naes2004_stategaymarriage2)

#Assault Weapon Ban
naes2004_assaultweapons<-svymean(~naes2004_assaultweapons, srs_design_survey, na.rm=T)
naes2004_assaultweapons
naes2004_assaultweapons2<-svyby(~naes2004_assaultweapons, ~group2, srs_design_survey, svymean, na.rm=T)
xtable(naes2004_assaultweapons2)

#Don’t Increase Minimum Wage
all_minwage<-svymean(~minimumwage_12dollars,srs_design_survey, na.rm=T)
all_minwage
all_minwage2<-svyby(~minimumwage_12dollars, ~group2, srs_design_survey, svymean, na.rm=T)
xtable(all_minwage2)

#Make Abortions Illegal
cces2016_abortionalwaysillegal<-svymean(~cces2016_abortionalwaysillegal, srs_design_survey, na.rm=T)
cces2016_abortionalwaysillegal
cces2016_abortionalwaysillegal2<-svyby(~cces2016_abortionalwaysillegal, ~group2, srs_design_survey, svymean, na.rm=T)
xtable(cces2016_abortionalwaysillegal2)

#Body Cameras on Police
cces2016_bodycameras<-svymean(~cces2016_bodycameras, srs_design_survey, na.rm=T)
cces2016_bodycameras
cces2016_bodycameras2<-svyby(~cces2016_bodycameras, ~group2, srs_design_survey, svymean, na.rm=T)
xtable(cces2016_bodycameras2)




























